﻿using Xejen.Languages;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Xejen.Common.Winform.Test
{
    public partial class LanguageForm : Form
    {
        private readonly ILanguageManager _languageManager;
        public LanguageForm(ILanguageManager languageManager)
        {
            _languageManager = languageManager;

            InitializeComponent();

            Localize();

            languageManager.LanguageChanged += LanguageManager_LanguageChanged;
        }

        private void LanguageManager_LanguageChanged(object sender, ILanguage e)
        {
            Localize();
        }

        private void Localize()
        {
            ButtonChangeLanguage.Text = _languageManager.CurrentLanguage["Buttons.ButtonMount"];
            FrmCallRepairText.Text = _languageManager.CurrentLanguage["Labels.mazhiyuan"];

            //测试多级
            var a = _languageManager.CurrentLanguage["DataSyncRecordsControl.PauseButton.Pause"];
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var currentLanguage = _languageManager.CurrentLanguage;

            LanguageComboBox.DataSource = _languageManager.GetLanguages().ToList();
            LanguageComboBox.DisplayMember = "Value";
            LanguageComboBox.ValueMember = "Key";

            LanguageComboBox.SelectedValue = currentLanguage.LanguageCode;

            SetCulture("en-US");

            button3.PerformClick();
            button3.IsSelected = true;
        }

        private void SetCulture(string cultureCode)
        {
            CultureInfo culture = new CultureInfo(cultureCode);
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetCulture("en-US");
            if (MessageBox.Show("确定 吗？") == DialogResult.OK)
            {

            }
            _languageManager.ChangeLanguage("EN");
        }

        private void LanguageComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LanguageComboBox.SelectedIndex == -1)
            {
                return;
            }

            var code = ((KeyValuePair<string, string>)LanguageComboBox.SelectedItem).Key;

            // 根据选择的语言设置文化
            if (code == "EN")
            {
                SetCulture("en-US");
            }
            else if (code == "CN")
            {
                SetCulture("zh-CN");
            }
            else if (code == "TW")
            {
                SetCulture("zh-TW");
            }

            _languageManager.ChangeLanguage(code);
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            var data = @"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
              <soap:Header>
                <LoginSoapHeader xmlns=""http://tempuri.org/"">
                  <EquUserID>admin</EquUserID>
                  <EquPassword>admin123</EquPassword>
                </LoginSoapHeader>
              </soap:Header>
              <soap:Body>
                <HelloWorld xmlns=""http://tempuri.org/"">
                  <body>
                    <EquipmentCode>${CCD_Ca_ofRetain1}</EquipmentCode>
                  </body>
                </HelloWorld>
              </soap:Body>
            </soap:Envelope>";

            //var data = @"
            //    {
            //        ""name"": ""John"",
            //        ""age"": 30,
            //        ""isStudent"": false,
            //        ""address"": null,
            //        ""courses"": [""Math"", ""Science""]
            //    }
            //";


            //var data = @"{""TaskId"":4524, ""Address"":{""HttpConfig"":{""ContentType"":""text/xml"", ""Encoding"":{""BodyName"":""utf-8"", ""EncodingName"":""Unicode (UTF-8)"", ""HeaderName"":""utf-8"", ""WebName"":""utf-8"", ""WindowsCodePage"":1200, ""IsBrowserDisplay"":true, ""IsBrowserSave"":true, ""IsMailNewsDisplay"":true, ""IsMailNewsSave"":true, ""IsSingleByte"":false, ""EncoderFallback"":{""DefaultString"":""�"", ""MaxCharCount"":1}, ""DecoderFallback"":{""DefaultString"":""�"", ""MaxCharCount"":1}, ""IsReadOnly"":true, ""CodePage"":65001}, ""EncodingName"":""UTF-8"", ""UserName"":""SUP_JCC01_CL01"", ""Password"":""CA@JCmesCL01"", ""Enable"":true, ""Timeout"":50000, ""HeartbeatConfig"":{""Enable"":true, ""Interval"":4567}, ""JsonSerializerOptions"":{""Converters"":[], ""TypeInfoResolver"":{""Modifiers"":[]}, ""TypeInfoResolverChain"":[], ""AllowTrailingCommas"":false, ""DefaultBufferSize"":16384, ""Encoder"":{""MaxOutputCharactersPerInputCharacter"":6}, ""IgnoreNullValues"":false, ""DefaultIgnoreCondition"":""Never"", ""NumberHandling"":""Strict"", ""PreferredObjectCreationHandling"":""Replace"", ""IgnoreReadOnlyProperties"":false, ""IgnoreReadOnlyFields"":false, ""IncludeFields"":false, ""MaxDepth"":0, ""PropertyNamingPolicy"":""System.Text.Json.JsonCamelCaseNamingPolicy"", ""PropertyNameCaseInsensitive"":true, ""ReadCommentHandling"":""Disallow"", ""UnknownTypeHandling"":""JsonElement"", ""UnmappedMemberHandling"":""Skip"", ""WriteIndented"":false, ""IsReadOnly"":true}, ""LogVerbosity"":""Normal""}, ""RequestAddress"":{""Value"":{""RequestUri"":""http://192.168.3.164:112/WebService1.asmx?op=HelloWorld""}, ""HasValue"":true}, ""Headers"":[""Username"",""Password""]}, ""TimeOut"":50000, ""Response"":{""Code"":200, ""IsSuccess"":true, ""ReturnData"":""<?xml version=\""1.0\"" encoding=\""utf-8\""?><soap:Envelope xmlns:soap=\""http://schemas.xmlsoap.org/soap/envelope/\"" xmlns:xsi=\""http://www.w3.org/2001/XMLSchema-instance\"" xmlns:xsd=\""http://www.w3.org/2001/XMLSchema\""><soap:Body><HelloWorldResponse xmlns=\""http://tempuri.org/\""><HelloWorldResult><Code>-1</Code><Msg>用户验证通过！</Msg></HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>""}}";


            //var data = "土木工程魂牵梦萦32大声道";

            Xejen.Editor.DataVisualizeForm form = new Editor.DataVisualizeForm(null, readWriteType: Editor.ReadWriteType.Write);
            if (form.ShowDialog() == DialogResult.OK)
            {
                var value = await form.GetDataAsync();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("被点击了");
        }
    }
}
